package pl.veneo.wigprofit.tools {
	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class TimeSpan {
		private var _totalMilliseconds : Number;
		public static const MILLISECONDS_IN_DAY : Number = 86400000;
		public static const MILLISECONDS_IN_HOUR : Number = 3600000;
		public static const MILLISECONDS_IN_MINUTE : Number = 60000;
		public static const MILLISECONDS_IN_SECOND : Number = 1000;

		public function TimeSpan(milliseconds : Number) {
			_totalMilliseconds = Math.floor(milliseconds);
		}

		public function get days() : int {
			return int(_totalMilliseconds / MILLISECONDS_IN_DAY);
		}

		public function get hours() : int {
			return int(_totalMilliseconds / MILLISECONDS_IN_HOUR) % 24;
		}

		public function get minutes() : int {
			return int(_totalMilliseconds / MILLISECONDS_IN_MINUTE) % 60;
		}

		public function get seconds() : int {
			return int(_totalMilliseconds / MILLISECONDS_IN_SECOND) % 60;
		}

		public function get milliseconds() : int {
			return int(_totalMilliseconds) % 1000;
		}

		public function get totalDays() : Number {
			return _totalMilliseconds / MILLISECONDS_IN_DAY;
		}

		public function get totalHours() : Number {
			return _totalMilliseconds / MILLISECONDS_IN_HOUR;
		}

		public function get totalMinutes() : Number {
			return _totalMilliseconds / MILLISECONDS_IN_MINUTE;
		}

		public function get totalSeconds() : Number {
			return _totalMilliseconds / MILLISECONDS_IN_SECOND;
		}

		public function get totalMilliseconds() : Number {
			return _totalMilliseconds;
		}

		public function add(date : Date) : Date {
			var ret : Date = new Date(date.time);
			ret.milliseconds += totalMilliseconds;

			return ret;
		}

		/**
		 * Creates a TimeSpan from the different between two dates
		 * 
		 * Note that start can be after end, but it will result in negative values. 
		 *  
		 * @param start The start date of the timespan
		 * @param end The end date of the timespan
		 * @return A TimeSpan that represents the difference between the dates
		 * 
		 */
		public static function fromDates(start : Date, end : Date) : TimeSpan {
			return new TimeSpan(end.time - start.time);
		}

		/**
		 * Creates a TimeSpan from the specified number of milliseconds
		 * @param milliseconds The number of milliseconds in the timespan
		 * @return A TimeSpan that represents the specified value
		 */
		public static function fromMilliseconds(milliseconds : Number) : TimeSpan {
			return new TimeSpan(milliseconds);
		}

		/**
		 * Creates a TimeSpan from the specified number of seconds
		 * @param seconds The number of seconds in the timespan
		 * @return A TimeSpan that represents the specified value
		 */
		public static function fromSeconds(seconds : Number) : TimeSpan {
			return new TimeSpan(seconds * MILLISECONDS_IN_SECOND);
		}

		/**
		 * Creates a TimeSpan from the specified number of minutes
		 * @param minutes The number of minutes in the timespan
		 * @return A TimeSpan that represents the specified value
		 */
		public static function fromMinutes(minutes : Number) : TimeSpan {
			return new TimeSpan(minutes * MILLISECONDS_IN_MINUTE);
		}

		/**
		 * Creates a TimeSpan from the specified number of hours
		 * @param hours The number of hours in the timespan
		 * @return A TimeSpan that represents the specified value
		 */
		public static function fromHours(hours : Number) : TimeSpan {
			return new TimeSpan(hours * MILLISECONDS_IN_HOUR);
		}

		/**
		 * Creates a TimeSpan from the specified number of days
		 * @param days The number of days in the timespan
		 * @return A TimeSpan that represents the specified value
		 */
		public static function fromDays(days : Number) : TimeSpan {
			return new TimeSpan(days * MILLISECONDS_IN_DAY);
		}
	}
}
